<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
     
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Details:details:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户" align="center" prop="sysUser.nickName" />
      <el-table-column label="订单编号" align="center" prop="orderId" />  
      <el-table-column label="商品名" align="center" prop="products.productName" />
      <el-table-column align="center" label="商品图片链接" width="120" :height="50" :show-overflow-tooltip="true" prop="image_url">
    <template scope="scope">
        <el-popover
                placement="right"
                title=""
                trigger="hover">
            <el-image slot="reference" :src="scope.row.products.image" :alt="scope.row.products.image" style="max-height: 200px;max-width: 200px"></el-image>
            <el-image :src="scope.row.products.image" ></el-image>
        </el-popover>
    </template>
</el-table-column>
      <el-table-column label="收获信息" align="center" prop="products.detail" show-overflow-tooltip="true">
  <template slot-scope="scope">
    <span v-html="removeParagraphTags(scope.row.products.detail)"></span>
  </template>
  <el-table-column label="省市区" align="center" prop="shippingAddresses.detailAddress" />
      <el-table-column label="收货地址" align="center" prop="shippingAddresses.address" />
      <el-table-column label="手机号码" align="center" prop="shippingAddresses.phoneNumber" />
      <el-table-column label="收货人姓名" align="center" prop="shippingAddresses.recipientName" />
</el-table-column>
      <el-table-column label="下单时间" align="center" prop="datatime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.datatime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
     
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单详情对话框 -->
   
  </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails } from "@/api/Details/details";

export default {
  name: "Details",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        productId: null,
        userId: null,
        addressid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "商品 ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户 ID不能为空", trigger: "blur" }
        ],
        addressid: [
          { required: true, message: "收货地址id不能为空", trigger: "blur" }
        ],
        datatime: [
          { required: true, message: "当前下单时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单详情列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    removeParagraphTags(description) {
    if (description) {
      return description.replace(/<\/?p>/g, ''); // 去掉 <p> 标签
    }
    return '';
  },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        productId: null,
        userId: null,
        addressid: null,
        isDeleted: null,
        datatime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单详情编号为"' + ids + '"的数据项？').then(function() {
        return delDetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('Details/details/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
